package com.wthink.hzgj.business

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.wthink.hzgj.BusApplication
import com.wthink.hzgj.R
import com.wthink.hzgj.business.adapter.BusinessAdapter
import com.wthink.hzgj.business.data.Business
import com.wthink.hzgj.business.viewmodel.BusinessViewModel
import com.wthink.hzgj.databinding.FragmentBusinessBinding
import com.wthink.hzgj.util.ClickUtil
import com.wthink.hzgj.util.adapter.OnItemClickListener
import com.wthink.hzgj.util.closeDefaultAnimator
import com.wthink.newMain.newBusiness.adapter.BusinessUpAdapter

class BusinessFragment : Fragment() {

    private val viewModel by viewModels<BusinessViewModel>()

    private lateinit var binding: FragmentBusinessBinding

    var business = ""

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_business,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this

        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        binding.back.setOnClickListener {
            it.findNavController().popBackStack()
        }

        var array: Array<String> = arrayOf(
            "排班", "交班", "加油", "加气", "充电", "维修", "包车", "终止任务"
        )

        val arrayList = ArrayList<Business>()
        for (item in array) {
            arrayList.add(Business(item))
        }

        //viewModel.start()
        binding.businessRecycler.layoutManager = GridLayoutManager(context, 4)
        binding.businessRecycler.adapter = BusinessUpAdapter().apply {
        this.update(arrayList)
        mItemClickListener = object : OnItemClickListener {
                override fun onItemClick(obj: Any, position: Int) {
                    val notFastClick = ClickUtil.isNotFastClick()
                    if (notFastClick) {
                       viewModel.updateBusiness((obj as String))
                    }
                }
            }
        }
        closeDefaultAnimator(binding.businessRecycler)
    }
}
